package com.kkwrite.regimen.provider.service.spokesman;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.kkwrite.regimen.common.entity.dto.item.ItemDetailDTO;
import com.kkwrite.regimen.common.entity.dto.picture.PictureDTO;
import com.kkwrite.regimen.common.entity.dto.spokesman.SpokesmanDTO;
import com.kkwrite.regimen.common.entity.dto.user.UserOutDTO;
import com.kkwrite.regimen.provider.domain.RegSpokesmanDO;
import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.repository.RegSpokesmanRepository;
import com.kkwrite.regimen.provider.service.item.ItemService;
import com.kkwrite.regimen.provider.service.picture.PictureService;
import com.kkwrite.regimen.provider.service.user.UserService;

/**
 *
 * @author jinying
 */
@Service("spokesmanService")
public class SpokesmanServiceImpl implements SpokesmanService {
	@Resource(name = "regUserService")
	private UserService userService;
	
	@Resource(name = "itemService")
	private ItemService itemService;
	
    @Resource(name = "regSpokesmanRepository")
    private  RegSpokesmanRepository regSpokesmanRepository;
    
	@Resource(name = "pictureService")
	private PictureService pictureService;

	@Override
	public List<SpokesmanDTO> querySpokesman(Long itemId) throws NumberFormatException, ServiceException {
		List<SpokesmanDTO> spokesmanDTOList = new ArrayList<>();
		List<RegSpokesmanDO> spokesmanDoList = regSpokesmanRepository.findAllByItemId(itemId);
		if (spokesmanDoList != null && spokesmanDoList.size()>0) {
			for (RegSpokesmanDO regSpokesmanDO : spokesmanDoList) {
				SpokesmanDTO spokesmanDTO = new SpokesmanDTO();
				BeanUtils.copyProperties(regSpokesmanDO, spokesmanDTO);
				// 查询用户
				UserOutDTO userDTO = userService.queryUser(regSpokesmanDO.getUserId() + "").getData();
				// 查项目
				ItemDetailDTO itemDetailDTO = itemService.getItemDetail(itemId);
				//查图片
				List<PictureDTO> pictures = pictureService.getOwerPictures(itemId, (short) 5);
				spokesmanDTO.setPictures(pictures);
				BeanUtils.copyProperties(userDTO, spokesmanDTO);
				BeanUtils.copyProperties(itemDetailDTO, spokesmanDTO);
				spokesmanDTOList.add(spokesmanDTO);
			}
		}		
		return  spokesmanDTOList;
	}

}
